{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "83c3ae56-34a9-42fe-94d1-488609d0fb4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.path import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3aceb54c-8dc4-4e79-952c-46d2d49313e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取Excel文件\n",
    "df_task = pd.read_excel('../data/Q1.xlsx')  \n",
    "df_member = pd.read_excel('../data/Q2.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ae40ad9f-76b7-4208-85d0-402d6940fd5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "eccept_index = [372, 482, 485, 486, 487, 488, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 538, 611, 612, 614, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 684, 685, 686, 688, 694, 695, 696, 697, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 723, 724, 725, 726, 727, 737, 740, 741, 742, 743, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 775, 778, 779, 780, 781, 784, 786, 801, 802, 803, 804, 805, 806, 807, 827, 832, 833]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6a0df80a-f68e-45c6-8224-0e576c72c976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况\n",
       "0    A0001  22.566142  113.980837  66.0       0\n",
       "1    A0002  22.686205  113.940525  65.5       0\n",
       "2    A0003  22.576512  113.957198  65.5       1\n",
       "3    A0004  22.564841  114.244571  75.0       0\n",
       "4    A0005  22.558888  113.950723  65.5       0\n",
       "..     ...        ...         ...   ...     ...\n",
       "652  A0829  23.179030  112.876192  80.0       1\n",
       "653  A0830  23.123411  113.151775  85.0       1\n",
       "654  A0831  23.044062  113.125784  65.5       0\n",
       "655  A0832  22.833262  113.280152  72.0       1\n",
       "656  A0835  23.123294  113.110382  85.0       1\n",
       "\n",
       "[657 rows x 5 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_task = df_task.drop(index=eccept_index)\n",
    "# 重置索引，丢弃旧的索引\n",
    "df_task = df_task.reset_index(drop=True)\n",
    "df_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3803483-1e2c-42b4-b6fb-c53886960c3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>会员编号</th>\n",
       "      <th>会员位置(GPS)</th>\n",
       "      <th>预订任务限额</th>\n",
       "      <th>预订任务开始时间</th>\n",
       "      <th>信誉值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>B0001</td>\n",
       "      <td>22.947097 113.679983</td>\n",
       "      <td>114</td>\n",
       "      <td>06:30:00</td>\n",
       "      <td>67997.3868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B0002</td>\n",
       "      <td>22.577792 113.966524</td>\n",
       "      <td>163</td>\n",
       "      <td>06:30:00</td>\n",
       "      <td>37926.5416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>B0003</td>\n",
       "      <td>23.192458 113.347272</td>\n",
       "      <td>139</td>\n",
       "      <td>06:30:00</td>\n",
       "      <td>27953.0363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B0004</td>\n",
       "      <td>23.255965 113.31875</td>\n",
       "      <td>98</td>\n",
       "      <td>06:30:00</td>\n",
       "      <td>25085.6986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B0005</td>\n",
       "      <td>33.65205 116.97047</td>\n",
       "      <td>66</td>\n",
       "      <td>06:30:00</td>\n",
       "      <td>20919.0667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1872</th>\n",
       "      <td>B1873</td>\n",
       "      <td>22.840505 113.277245</td>\n",
       "      <td>1</td>\n",
       "      <td>08:00:00</td>\n",
       "      <td>0.0124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1873</th>\n",
       "      <td>B1874</td>\n",
       "      <td>23.069415 113.287606</td>\n",
       "      <td>1</td>\n",
       "      <td>08:00:00</td>\n",
       "      <td>0.0121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1874</th>\n",
       "      <td>B1875</td>\n",
       "      <td>23.333446 113.301736</td>\n",
       "      <td>1</td>\n",
       "      <td>08:00:00</td>\n",
       "      <td>0.0062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1875</th>\n",
       "      <td>B1876</td>\n",
       "      <td>22.693506 113.994101</td>\n",
       "      <td>1</td>\n",
       "      <td>08:00:00</td>\n",
       "      <td>0.0036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1876</th>\n",
       "      <td>B1877</td>\n",
       "      <td>23.133238 113.239864</td>\n",
       "      <td>1</td>\n",
       "      <td>08:00:00</td>\n",
       "      <td>0.0001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1877 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       会员编号             会员位置(GPS)  预订任务限额  预订任务开始时间         信誉值\n",
       "0     B0001  22.947097 113.679983     114  06:30:00  67997.3868\n",
       "1     B0002  22.577792 113.966524     163  06:30:00  37926.5416\n",
       "2     B0003  23.192458 113.347272     139  06:30:00  27953.0363\n",
       "3     B0004   23.255965 113.31875      98  06:30:00  25085.6986\n",
       "4     B0005    33.65205 116.97047      66  06:30:00  20919.0667\n",
       "...     ...                   ...     ...       ...         ...\n",
       "1872  B1873  22.840505 113.277245       1  08:00:00      0.0124\n",
       "1873  B1874  23.069415 113.287606       1  08:00:00      0.0121\n",
       "1874  B1875  23.333446 113.301736       1  08:00:00      0.0062\n",
       "1875  B1876  22.693506 113.994101       1  08:00:00      0.0036\n",
       "1876  B1877  23.133238 113.239864       1  08:00:00      0.0001\n",
       "\n",
       "[1877 rows x 5 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_member"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15e59426-5485-4a2b-938e-1290bcdb598d",
   "metadata": {},
   "source": [
    "基准值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5d73dd56-321f-4f52-9a3c-f5290941a965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(df_task['任务标价'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2396a25c-59dd-4962-933b-9ef6b5a47ca3",
   "metadata": {},
   "source": [
    "会员因素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "04664d0a-917d-4ed3-8344-0275c64cd4f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "def euclidean_distance(lat1, lon1, lat2, lon2):\n",
    "    # 将纬度和经度转换为相同单位\n",
    "    delta_lat = lat2 - lat1\n",
    "    delta_lon = lon2 - lon1\n",
    "    \n",
    "    # 简单的欧几里得距离\n",
    "    return math.sqrt(delta_lat ** 2 + delta_lon ** 2) * 111  # 1度大约为111公里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a78445bf-a6d0-49dc-9a2d-d5e9f61e357e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 统计每个任务点1km内的顾客数量\n",
    "result_member = []\n",
    "\n",
    "for index, task in df_task.iterrows():\n",
    "    task_lat, task_lon = task['任务gps纬度'], task['任务gps经度']\n",
    "    count = 0\n",
    "    score = 0\n",
    "    members_within_1km = []  # 用于存储1km内的顾客信息\n",
    "\n",
    "    # 遍历2km内的所有顾客\n",
    "    for _, member in df_member.iterrows():\n",
    "        member_lat, member_lon = map(float, member['会员位置(GPS)'].split())\n",
    "        distance = euclidean_distance(task_lat, task_lon, member_lat, member_lon)\n",
    "        \n",
    "        if distance <= 2:\n",
    "            count += 1\n",
    "            members_within_1km.append({\n",
    "                '会员ID': member['会员编号'],  # 可选，用于标识会员\n",
    "                '信誉分': member['信誉值'],\n",
    "                '距离': distance\n",
    "            })\n",
    "            # 分数累积\n",
    "            score += (member['预订任务限额'] + math.sqrt(member['信誉值'])) * (1-distance) * 0.01\n",
    "\n",
    "    result_member.append({\n",
    "        '任务号码': task['任务号码'], \n",
    "        '1km内顾客数量': count,\n",
    "        '顾客信息': members_within_1km,  # 存储顾客的信誉分和距离\n",
    "        '该任务点的会员因子分数': score      # 该任务点的会员因子分数\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c1b658ed-2b36-48d3-b387-a34eeb97a5fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>1km内顾客数量</th>\n",
       "      <th>顾客信息</th>\n",
       "      <th>该任务点的会员因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>2</td>\n",
       "      <td>[{'会员ID': 'B0355', '信誉分': 52.5572, '距离': 1.833...</td>\n",
       "      <td>-0.175175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22</td>\n",
       "      <td>[{'会员ID': 'B0262', '信誉分': 82.9086, '距离': 1.190...</td>\n",
       "      <td>0.023039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>9</td>\n",
       "      <td>[{'会员ID': 'B0002', '信誉分': 37926.5416, '距离': 1....</td>\n",
       "      <td>-0.302747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'会员ID': 'B1751', '信誉分': 0.3636, '距离': 1.1200...</td>\n",
       "      <td>-0.001924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>8</td>\n",
       "      <td>[{'会员ID': 'B0012', '信誉分': 10957.5811, '距离': 1....</td>\n",
       "      <td>-0.825690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>3</td>\n",
       "      <td>[{'会员ID': 'B0337', '信誉分': 59.7694, '距离': 1.442...</td>\n",
       "      <td>-0.054424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>4</td>\n",
       "      <td>[{'会员ID': 'B0137', '信誉分': 225.3733, '距离': 1.86...</td>\n",
       "      <td>-0.546518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>10</td>\n",
       "      <td>[{'会员ID': 'B0041', '信誉分': 1523.3223, '距离': 1.0...</td>\n",
       "      <td>-0.138593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>4</td>\n",
       "      <td>[{'会员ID': 'B0149', '信誉分': 199.2313, '距离': 1.79...</td>\n",
       "      <td>-0.681070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'会员ID': 'B1428', '信誉分': 2.0, '距离': 0.1522549...</td>\n",
       "      <td>0.028944</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码  1km内顾客数量                                               顾客信息  \\\n",
       "0    A0001         2  [{'会员ID': 'B0355', '信誉分': 52.5572, '距离': 1.833...   \n",
       "1    A0002        22  [{'会员ID': 'B0262', '信誉分': 82.9086, '距离': 1.190...   \n",
       "2    A0003         9  [{'会员ID': 'B0002', '信誉分': 37926.5416, '距离': 1....   \n",
       "3    A0004         1  [{'会员ID': 'B1751', '信誉分': 0.3636, '距离': 1.1200...   \n",
       "4    A0005         8  [{'会员ID': 'B0012', '信誉分': 10957.5811, '距离': 1....   \n",
       "..     ...       ...                                                ...   \n",
       "652  A0829         3  [{'会员ID': 'B0337', '信誉分': 59.7694, '距离': 1.442...   \n",
       "653  A0830         4  [{'会员ID': 'B0137', '信誉分': 225.3733, '距离': 1.86...   \n",
       "654  A0831        10  [{'会员ID': 'B0041', '信誉分': 1523.3223, '距离': 1.0...   \n",
       "655  A0832         4  [{'会员ID': 'B0149', '信誉分': 199.2313, '距离': 1.79...   \n",
       "656  A0835         1  [{'会员ID': 'B1428', '信誉分': 2.0, '距离': 0.1522549...   \n",
       "\n",
       "     该任务点的会员因子分数  \n",
       "0      -0.175175  \n",
       "1       0.023039  \n",
       "2      -0.302747  \n",
       "3      -0.001924  \n",
       "4      -0.825690  \n",
       "..           ...  \n",
       "652    -0.054424  \n",
       "653    -0.546518  \n",
       "654    -0.138593  \n",
       "655    -0.681070  \n",
       "656     0.028944  \n",
       "\n",
       "[657 rows x 4 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为DataFrame并显示\n",
    "df_result_member = pd.DataFrame(result_member)\n",
    "df_result_member"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f25dd614-f594-466b-9950-210eaf4bf860",
   "metadata": {},
   "source": [
    "竞争因素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a90647f5-40b5-4aa6-bc83-da375216eb03",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 统计每个任务点2km内的其他任务点数量\n",
    "result_cpt = []\n",
    "\n",
    "for index, task in df_task.iterrows():\n",
    "    task_lat, task_lon = task['任务gps纬度'], task['任务gps经度']\n",
    "    count = 0\n",
    "    score = 0\n",
    "    tasks_within_1km = []  # 用于存储1km内的顾客信息\n",
    "\n",
    "    # 遍历1km内的所有其他任务点\n",
    "    for _, oppotask in df_task.iterrows():\n",
    "        oppotask_lat, oppotask_lon = oppotask['任务gps纬度'], oppotask['任务gps经度']\n",
    "        distance = euclidean_distance(task_lat, task_lon, oppotask_lat, oppotask_lon)\n",
    "        \n",
    "        if distance <= 2 and distance != 0:\n",
    "            count += 1\n",
    "            tasks_within_1km.append({\n",
    "                '任务号码': oppotask['任务号码'],  # 可选，用于标识会员\n",
    "                '距离': distance\n",
    "            })\n",
    "            # 分数累积\n",
    "            score += min(math.sqrt(1 / distance) * 0.1, 5)\n",
    "\n",
    "    result_cpt.append({\n",
    "        '其他任务ID': task['任务号码'], \n",
    "        '1km内其他任务点数量': count,\n",
    "        '其他任务点的信息': tasks_within_1km,  # 存储顾客的信誉分和距离\n",
    "        '该任务点的竞争因子分数': score      # 该任务点的竞争因子分数\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "28606749-fe1c-40ff-b807-04bf8ae1ddbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>其他任务ID</th>\n",
       "      <th>1km内其他任务点数量</th>\n",
       "      <th>其他任务点的信息</th>\n",
       "      <th>该任务点的竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>3</td>\n",
       "      <td>[{'任务号码': 'A0029', '距离': 0.20568449470847416},...</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>2</td>\n",
       "      <td>[{'任务号码': 'A0364', '距离': 0.6155373624668447}, ...</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>2</td>\n",
       "      <td>[{'任务号码': 'A0008', '距离': 1.5265281241126067}, ...</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'任务号码': 'A0006', '距离': 0.7422286186792472}]</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>3</td>\n",
       "      <td>[{'任务号码': 'A0008', '距离': 0.7796263532124108}, ...</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'任务号码': 'A0542', '距离': 0.35025617039742757}]</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'任务号码': 'A0784', '距离': 1.4498734948631613}]</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>7</td>\n",
       "      <td>[{'任务号码': 'A0484', '距离': 1.406211984488132}, {...</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'任务号码': 'A0668', '距离': 1.5330031297967097}]</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>3</td>\n",
       "      <td>[{'任务号码': 'A0775', '距离': 1.7142837280557874}, ...</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    其他任务ID  1km内其他任务点数量                                           其他任务点的信息  \\\n",
       "0    A0001            3  [{'任务号码': 'A0029', '距离': 0.20568449470847416},...   \n",
       "1    A0002            2  [{'任务号码': 'A0364', '距离': 0.6155373624668447}, ...   \n",
       "2    A0003            2  [{'任务号码': 'A0008', '距离': 1.5265281241126067}, ...   \n",
       "3    A0004            1      [{'任务号码': 'A0006', '距离': 0.7422286186792472}]   \n",
       "4    A0005            3  [{'任务号码': 'A0008', '距离': 0.7796263532124108}, ...   \n",
       "..     ...          ...                                                ...   \n",
       "652  A0829            1     [{'任务号码': 'A0542', '距离': 0.35025617039742757}]   \n",
       "653  A0830            1      [{'任务号码': 'A0784', '距离': 1.4498734948631613}]   \n",
       "654  A0831            7  [{'任务号码': 'A0484', '距离': 1.406211984488132}, {...   \n",
       "655  A0832            1      [{'任务号码': 'A0668', '距离': 1.5330031297967097}]   \n",
       "656  A0835            3  [{'任务号码': 'A0775', '距离': 1.7142837280557874}, ...   \n",
       "\n",
       "     该任务点的竞争因子分数  \n",
       "0       0.415050  \n",
       "1       0.238261  \n",
       "2       0.201153  \n",
       "3       0.116073  \n",
       "4       0.294938  \n",
       "..           ...  \n",
       "652     0.168969  \n",
       "653     0.083049  \n",
       "654     0.624091  \n",
       "655     0.080766  \n",
       "656     0.262838  \n",
       "\n",
       "[657 rows x 4 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为DataFrame并显示\n",
    "df_result_cpt = pd.DataFrame(result_cpt)\n",
    "df_result_cpt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72f3be20-077c-43e6-b47b-8b6eaa758a55",
   "metadata": {},
   "source": [
    "训练完，得到 k、w，然后再定价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "03cb3c02-5805-443b-a881-4242ffdc694d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>预测合理价格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况     预测合理价格\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378\n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500\n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029\n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746\n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242\n",
       "..     ...        ...         ...   ...     ...        ...\n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724\n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655\n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490\n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482\n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909\n",
       "\n",
       "[657 rows x 6 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price = []  # 初始化价格列表\n",
    "k = 5\n",
    "w = 5\n",
    "\n",
    "for i in df_task.index:  # 使用 .index 确保按索引遍历\n",
    "    # 假设 df_result_member 是 DataFrame，并且 i 是任务点的标识符\n",
    "    member_factor = df_result_member.loc[i, '该任务点的会员因子分数']\n",
    "    competition_factor = df_result_cpt.loc[i, '该任务点的竞争因子分数']\n",
    "    \n",
    "    # 计算该任务点的价格\n",
    "    task_price = 65 + k * member_factor + w * competition_factor\n",
    "    \n",
    "    # 将计算的价格添加到 price 列表\n",
    "    price.append(task_price)\n",
    "\n",
    "# 如果你希望将 price 添加为 df_task 的一列，可以使用以下代码\n",
    "df_task['预测合理价格'] = price\n",
    "df_task"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b807ee33-ff18-459b-899c-cc51f0cdd74a",
   "metadata": {},
   "source": [
    "校验定价区间合理性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c28f460f-2ea9-455b-bed4-9cf3cc81f6fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40.8439175444561"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_task['预测合理价格'].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a90cdf67-b028-429d-b0ea-81e6e0f7e1ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "84.73058351199023"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_task['预测合理价格'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bd99eac6-1660-4daa-8479-ade2b052a198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65.85083980383452"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_task['预测合理价格'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "868ec50d-bfb4-4786-917f-c65bb013eecb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 预处理数据\n",
    "# 将会员位置(GPS)拆分为经度和纬度\n",
    "df_member[['会员纬度', '会员经度']] = df_member['会员位置(GPS)'].str.split(expand=True).astype(float)\n",
    "\n",
    "# 选择任务数据和会员数据的特征进行合并\n",
    "# 例如：根据任务ID或其他关联特征合并数据\n",
    "# 这里假设任务表和会员表已经预先关联好\n",
    "df = pd.merge(df_task, df_member, left_on='任务gps纬度', right_on='会员纬度', how='left')\n",
    "\n",
    "# 特征选择（可以根据你的数据进行调整）\n",
    "X = df[['任务gps纬度', '任务gps经度', '任务标价', '信誉值', '预订任务限额']]\n",
    "y = df['任务执行情况']  # 这是你要预测的目标变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "377838a8-d877-4b4a-a8f0-257aa73c77ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.8257575757575758\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.84      0.80      0.82        66\n",
      "           1       0.81      0.85      0.83        66\n",
      "\n",
      "    accuracy                           0.83       132\n",
      "   macro avg       0.83      0.83      0.83       132\n",
      "weighted avg       0.83      0.83      0.83       132\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 2. 数据分割为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 模型训练\n",
    "model = RandomForestClassifier(random_state=42)\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# 4. 预测\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "# 5. 模型评估\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(f\"Accuracy: {accuracy}\")\n",
    "print(\"Classification Report:\")\n",
    "print(report)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65f308be-cf9d-481a-a2f5-d0fc737852ab",
   "metadata": {},
   "source": [
    "## 原本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5f59679c-5fdf-4d41-922d-5a2f9a08bd41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>预测合理价格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况     预测合理价格\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378\n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500\n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029\n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746\n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242\n",
       "..     ...        ...         ...   ...     ...        ...\n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724\n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655\n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490\n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482\n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909\n",
       "\n",
       "[657 rows x 6 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_task"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a03a562-fa93-4a01-84ac-33d40fffe294",
   "metadata": {},
   "source": [
    "在原本任务表Q1中，加上不含k,w的那两列原始影响数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f82a6b52-d96b-4c5b-85c4-3027dfc9a7bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设 df_result_member 和 df_result_cpt 是单列的 DataFrame，且与 df_task 具有相同的索引\n",
    "\n",
    "# 首先将会员因子分数和竞争因子分数合并到 df_task 中\n",
    "df_new_task = df_task.copy()  # 复制原始任务数据\n",
    "\n",
    "# 添加会员因子分数列\n",
    "df_new_task['会员因子分数'] = df_result_member['该任务点的会员因子分数']\n",
    "\n",
    "# 添加竞争因子分数列\n",
    "df_new_task['竞争因子分数'] = df_result_cpt['该任务点的竞争因子分数']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "577a929f-e4bf-4809-8be4-987d32e42720",
   "metadata": {},
   "source": [
    "此时 df_new_task 为加上特征后的 df，因此可以开始训练！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "94db15f4-0313-49d5-bfd4-960c74268d3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>预测合理价格</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况     预测合理价格    会员因子分数    竞争因子分数\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378 -0.175175  0.415050\n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500  0.023039  0.238261\n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029 -0.302747  0.201153\n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746 -0.001924  0.116073\n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242 -0.825690  0.294938\n",
       "..     ...        ...         ...   ...     ...        ...       ...       ...\n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724 -0.054424  0.168969\n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655 -0.546518  0.083049\n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490 -0.138593  0.624091\n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482 -0.681070  0.080766\n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909  0.028944  0.262838\n",
       "\n",
       "[657 rows x 8 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new_task"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f8651a0-c415-4c43-8ecf-b4ec8880df7a",
   "metadata": {},
   "source": [
    "## 预测完成率\n",
    "#### 先随机森林训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "99600c95-34c5-4409-94d3-09a4cd8a420d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.7727272727272727\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.79      0.74      0.77        66\n",
      "           1       0.76      0.80      0.78        66\n",
      "\n",
      "    accuracy                           0.77       132\n",
      "   macro avg       0.77      0.77      0.77       132\n",
      "weighted avg       0.77      0.77      0.77       132\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 准备数据\n",
    "# X 是特征集，选择 '任务标价'、'会员因子分数' 和 '竞争因子分数' 列\n",
    "X = df_new_task[['任务gps纬度', '任务gps经度', '任务标价', '会员因子分数', '竞争因子分数']]\n",
    "\n",
    "# y 是目标变量，选择 '任务执行情况' 列\n",
    "y = df_new_task['任务执行情况']\n",
    "\n",
    "# 2. 数据分割为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 模型训练\n",
    "model = RandomForestClassifier(n_estimators=200, random_state=42)\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# 4. 预测\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "# 5. 模型评估\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(f\"Accuracy: {accuracy}\")\n",
    "print(\"Classification Report:\")\n",
    "print(report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c2a0d14e-ba3b-43bc-8e4f-e2dda3c63c0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cross-validated accuracy scores: [0.68181818 0.67424242 0.58015267 0.67938931 0.54198473]\n",
      "Mean cross-validated accuracy: 0.6315174647235716\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 准备数据\n",
    "# X 是特征集，选择 '任务gps纬度'、'任务gps经度'、'任务标价'、'会员因子分数' 和 '竞争因子分数' 列\n",
    "X = df_new_task[['任务gps纬度', '任务gps经度', '任务标价', '会员因子分数', '竞争因子分数']]\n",
    "\n",
    "# y 是目标变量，选择 '任务执行情况' 列\n",
    "y = df_new_task['任务执行情况']\n",
    "\n",
    "# 2. 定义模型\n",
    "model = RandomForestClassifier(n_estimators=50, random_state=42)\n",
    "\n",
    "# 3. 使用 K 折交叉验证进行模型评估（默认使用5折交叉验证）\n",
    "# cross_val_score 返回的是每一折的准确率，我们取平均值\n",
    "scores = cross_val_score(model, X, y, cv=5, scoring='accuracy')\n",
    "\n",
    "# 4. 打印交叉验证的准确率\n",
    "print(f\"Cross-validated accuracy scores: {scores}\")\n",
    "print(f\"Mean cross-validated accuracy: {scores.mean()}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6faf2fdc-e4ba-4078-a0d4-98f5f7298185",
   "metadata": {},
   "source": [
    "调参：树的棵树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7681361e-8892-4b11-aa69-d31198d2e17e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best n_estimators: 60 with Accuracy: 0.803030303030303\n",
      "n_estimators: 10, Accuracy: 0.75\n",
      "n_estimators: 20, Accuracy: 0.7651515151515151\n",
      "n_estimators: 30, Accuracy: 0.7803030303030303\n",
      "n_estimators: 40, Accuracy: 0.7803030303030303\n",
      "n_estimators: 50, Accuracy: 0.7878787878787878\n",
      "n_estimators: 60, Accuracy: 0.803030303030303\n",
      "n_estimators: 70, Accuracy: 0.803030303030303\n",
      "n_estimators: 80, Accuracy: 0.7803030303030303\n",
      "n_estimators: 90, Accuracy: 0.7803030303030303\n",
      "n_estimators: 100, Accuracy: 0.7651515151515151\n",
      "n_estimators: 110, Accuracy: 0.7651515151515151\n",
      "n_estimators: 120, Accuracy: 0.7727272727272727\n",
      "n_estimators: 130, Accuracy: 0.7575757575757576\n",
      "n_estimators: 140, Accuracy: 0.7651515151515151\n",
      "n_estimators: 150, Accuracy: 0.7575757575757576\n",
      "n_estimators: 160, Accuracy: 0.7575757575757576\n",
      "n_estimators: 170, Accuracy: 0.7727272727272727\n",
      "n_estimators: 180, Accuracy: 0.75\n",
      "n_estimators: 190, Accuracy: 0.7727272727272727\n",
      "n_estimators: 200, Accuracy: 0.7727272727272727\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 1. 准备数据\n",
    "X = df_new_task[['任务gps纬度', '任务gps经度', '任务标价', '会员因子分数', '竞争因子分数']]\n",
    "y = df_new_task['任务执行情况']\n",
    "\n",
    "# 2. 数据分割为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 调参\n",
    "best_accuracy = 0\n",
    "best_n_estimators = 0\n",
    "accuracies = []\n",
    "\n",
    "for n in range(10, 201, 10):\n",
    "    model = RandomForestClassifier(n_estimators=n, random_state=42)\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict(X_test)\n",
    "    accuracy = accuracy_score(y_test, y_pred)\n",
    "    accuracies.append((n, accuracy))\n",
    "    \n",
    "    if accuracy > best_accuracy:\n",
    "        best_accuracy = accuracy\n",
    "        best_n_estimators = n\n",
    "\n",
    "# 输出最佳结果\n",
    "print(f\"Best n_estimators: {best_n_estimators} with Accuracy: {best_accuracy}\")\n",
    "\n",
    "# 打印每个 n_estimators 对应的准确率\n",
    "for n, accuracy in accuracies:\n",
    "    print(f\"n_estimators: {n}, Accuracy: {accuracy}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a68606e-567b-4b43-b406-0dea0c0874fb",
   "metadata": {},
   "source": [
    "新的完成率预测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0096487-a285-4c12-b8bb-db9372256ef5",
   "metadata": {},
   "source": [
    "复原列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4be16d19-5e9f-49d4-849d-c480e8daba56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况       任务标价    会员因子分数    竞争因子分数\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378 -0.175175  0.415050\n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500  0.023039  0.238261\n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029 -0.302747  0.201153\n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746 -0.001924  0.116073\n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242 -0.825690  0.294938\n",
       "..     ...        ...         ...   ...     ...        ...       ...       ...\n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724 -0.054424  0.168969\n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655 -0.546518  0.083049\n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490 -0.138593  0.624091\n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482 -0.681070  0.080766\n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909  0.028944  0.262838\n",
       "\n",
       "[657 rows x 8 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取列名列表\n",
    "columns = df_new_task.columns.to_list()\n",
    "\n",
    "# 获取第一个 '任务标价' 的索引位置\n",
    "first_index = columns.index('预测合理价格')\n",
    "\n",
    "# 重命名第一个 '任务标价' 列的名称\n",
    "df_new_task.columns.values[first_index] = '任务标价'\n",
    "\n",
    "# 查看修改后的 DataFrame\n",
    "df_new_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "95edb690-4637-4a01-929f-3b51aebc1762",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       任务gps纬度     任务gps经度       任务标价    会员因子分数    竞争因子分数\n",
       "0    22.566142  113.980837  66.199378 -0.175175  0.415050\n",
       "1    22.686205  113.940525  66.306500  0.023039  0.238261\n",
       "2    22.576512  113.957198  64.492029 -0.302747  0.201153\n",
       "3    22.564841  114.244571  65.570746 -0.001924  0.116073\n",
       "4    22.558888  113.950723  62.346242 -0.825690  0.294938\n",
       "..         ...         ...        ...       ...       ...\n",
       "652  23.179030  112.876192  65.572724 -0.054424  0.168969\n",
       "653  23.123411  113.151775  62.682655 -0.546518  0.083049\n",
       "654  23.044062  113.125784  67.427490 -0.138593  0.624091\n",
       "655  22.833262  113.280152  61.998482 -0.681070  0.080766\n",
       "656  23.123294  113.110382  66.458909  0.028944  0.262838\n",
       "\n",
       "[657 rows x 5 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 准备新的特征集 X（使用 '预测合理价格' 列）\n",
    "X_new = df_new_task[['任务gps纬度', '任务gps经度', '预测合理价格', '会员因子分数', '竞争因子分数']]\n",
    "X_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a20b23a7-2094-41ce-a821-ecee34ca3916",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       任务gps纬度     任务gps经度       任务标价    会员因子分数    竞争因子分数\n",
       "0    22.566142  113.980837  66.199378 -0.175175  0.415050\n",
       "1    22.686205  113.940525  66.306500  0.023039  0.238261\n",
       "2    22.576512  113.957198  64.492029 -0.302747  0.201153\n",
       "3    22.564841  114.244571  65.570746 -0.001924  0.116073\n",
       "4    22.558888  113.950723  62.346242 -0.825690  0.294938\n",
       "..         ...         ...        ...       ...       ...\n",
       "652  23.179030  112.876192  65.572724 -0.054424  0.168969\n",
       "653  23.123411  113.151775  62.682655 -0.546518  0.083049\n",
       "654  23.044062  113.125784  67.427490 -0.138593  0.624091\n",
       "655  22.833262  113.280152  61.998482 -0.681070  0.080766\n",
       "656  23.123294  113.110382  66.458909  0.028944  0.262838\n",
       "\n",
       "[657 rows x 5 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将 '预测合理价格' 列重命名为 '任务标价'\n",
    "X_new = X_new.rename(columns={'预测合理价格': '任务标价'})\n",
    "# X_new = X_new.rename(columns={'任务标价': '预测合理价格'})\n",
    "X_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d6d82e0e-bb9f-4118-b1d4-57101c1c3a55",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "      <th>预测任务执行情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况       任务标价    会员因子分数  \\\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378 -0.175175   \n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500  0.023039   \n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029 -0.302747   \n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746 -0.001924   \n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242 -0.825690   \n",
       "..     ...        ...         ...   ...     ...        ...       ...   \n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724 -0.054424   \n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655 -0.546518   \n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490 -0.138593   \n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482 -0.681070   \n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909  0.028944   \n",
       "\n",
       "       竞争因子分数  预测任务执行情况  \n",
       "0    0.415050         0  \n",
       "1    0.238261         0  \n",
       "2    0.201153         0  \n",
       "3    0.116073         0  \n",
       "4    0.294938         0  \n",
       "..        ...       ...  \n",
       "652  0.168969         1  \n",
       "653  0.083049         1  \n",
       "654  0.624091         0  \n",
       "655  0.080766         1  \n",
       "656  0.262838         1  \n",
       "\n",
       "[657 rows x 9 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 使用已经训练好的模型进行预测\n",
    "# 假设 model 是你之前训练好的模型\n",
    "y_new_pred = model.predict(X_new)\n",
    "\n",
    "# 3. 将新的预测结果存储到 df_new_task 的 '预测任务执行情况' 列中\n",
    "df_new_task['预测任务执行情况'] = y_new_pred\n",
    "\n",
    "# 4. 输出结果查看\n",
    "df_new_task\n",
    "# print(df_new_task[['任务gps纬度', '任务gps经度', '预测合理价格', '会员因子分数', '竞争因子分数', '预测任务执行情况']].head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4d5bdbd0-607f-4d8e-a77b-c0cef074d242",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "      <th>预测任务执行情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况       任务标价    会员因子分数  \\\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378 -0.175175   \n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500  0.023039   \n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029 -0.302747   \n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746 -0.001924   \n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242 -0.825690   \n",
       "..     ...        ...         ...   ...     ...        ...       ...   \n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724 -0.054424   \n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655 -0.546518   \n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490 -0.138593   \n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482 -0.681070   \n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909  0.028944   \n",
       "\n",
       "       竞争因子分数  预测任务执行情况  \n",
       "0    0.415050         0  \n",
       "1    0.238261         0  \n",
       "2    0.201153         0  \n",
       "3    0.116073         0  \n",
       "4    0.294938         0  \n",
       "..        ...       ...  \n",
       "652  0.168969         1  \n",
       "653  0.083049         1  \n",
       "654  0.624091         0  \n",
       "655  0.080766         1  \n",
       "656  0.262838         1  \n",
       "\n",
       "[657 rows x 9 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "733eef77-7488-4ce8-9bb2-0ed9a2acf96c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "356"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new_task['预测任务执行情况'].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4bb9c7c-bf88-4f33-ad3e-07f1203df464",
   "metadata": {},
   "source": [
    "### 调节k,w\n",
    "由于第一问的结果已知，因此使用这些数据来训练，调参，获得最佳的树的个数  \n",
    "之后调节k,w的值，使得df_new_task['预测任务执行情况'].sum()的值最大，即任务完成率最高  \n",
    "在X_new里面调整就行，省得改来改去乱了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6bc35c7b-2d58-4116-af5d-0a54ed6ee2f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# X_new = X_new.drop(columns=['预测合理价格'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "fe9a4b13-b625-493e-ad98-a959c11b81f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       任务gps纬度     任务gps经度       任务标价    会员因子分数    竞争因子分数\n",
       "0    22.566142  113.980837  66.199378 -0.175175  0.415050\n",
       "1    22.686205  113.940525  66.306500  0.023039  0.238261\n",
       "2    22.576512  113.957198  64.492029 -0.302747  0.201153\n",
       "3    22.564841  114.244571  65.570746 -0.001924  0.116073\n",
       "4    22.558888  113.950723  62.346242 -0.825690  0.294938\n",
       "..         ...         ...        ...       ...       ...\n",
       "652  23.179030  112.876192  65.572724 -0.054424  0.168969\n",
       "653  23.123411  113.151775  62.682655 -0.546518  0.083049\n",
       "654  23.044062  113.125784  67.427490 -0.138593  0.624091\n",
       "655  22.833262  113.280152  61.998482 -0.681070  0.080766\n",
       "656  23.123294  113.110382  66.458909  0.028944  0.262838\n",
       "\n",
       "[657 rows x 5 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b5cfb105-db36-45af-9c0a-faba03ca1ac4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最好的 k 为： 1 w 为： 6 res 为： 363\n"
     ]
    }
   ],
   "source": [
    "# 初始化参数\n",
    "best_k = 1\n",
    "best_w = 1\n",
    "best_res = 0\n",
    "\n",
    "# 网格搜索 k 和 w 的值\n",
    "for k in range(1, 11):  # 语法修改：range(1, 11) 代表从 1 到 10\n",
    "    for w in range(1, 11):\n",
    "        \n",
    "        price = []  # 初始化价格列表\n",
    "        \n",
    "        for i in df_new_task.index:  # 使用 .index 确保按索引遍历\n",
    "            # 获取会员因子分数和竞争因子分数\n",
    "            m_factor = df_new_task.loc[i, '会员因子分数']\n",
    "            c_factor = df_new_task.loc[i, '竞争因子分数']\n",
    "            \n",
    "            # 计算该任务点的价格\n",
    "            task_price = 65 + k * m_factor + w * c_factor\n",
    "            \n",
    "            # 将计算的价格添加到 price 列表\n",
    "            price.append(task_price)\n",
    "        \n",
    "        # 更新 '预测合理价格' 列\n",
    "        X_new['任务标价'] = price\n",
    "        \n",
    "        # 使用训练好的模型进行预测\n",
    "        y_new_pred = model.predict(X_new)\n",
    "        \n",
    "        # 将新的预测结果存储到 df_new_task 的 '预测任务执行情况' 列中\n",
    "        df_new_task['预测任务执行情况'] = y_new_pred\n",
    "        \n",
    "        # 计算当前配置下预测的任务执行情况的总和（比如任务完成的数量）\n",
    "        res = df_new_task['预测任务执行情况'].sum()\n",
    "        \n",
    "        # 更新最佳参数和结果\n",
    "        if res > best_res:\n",
    "            best_k = k\n",
    "            best_w = w\n",
    "            best_res = res\n",
    "\n",
    "print('最好的 k 为：', best_k, 'w 为：', best_w, 'res 为：', best_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "71d233dd-08fa-4c69-82bd-db5c3f356e04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最好的 k 为： 1 w 为： 6 res 为： 363\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 网格搜索 k 和 w 的值，步长为 0.2\n",
    "for k in np.arange(0, 2, 0.05):  # np.arange(1, 11, 0.2) 代表从 1 到 10，步长为 0.2\n",
    "    for w in np.arange(0, 2, 0.05):\n",
    "        \n",
    "        price = []  # 初始化价格列表\n",
    "        \n",
    "        for i in df_new_task.index:  # 使用 .index 确保按索引遍历\n",
    "            # 获取会员因子分数和竞争因子分数\n",
    "            m_factor = df_new_task.loc[i, '会员因子分数']\n",
    "            c_factor = df_new_task.loc[i, '竞争因子分数']\n",
    "            \n",
    "            # 计算该任务点的价格\n",
    "            task_price = 65 + k * m_factor + w * c_factor\n",
    "            \n",
    "            # 将计算的价格添加到 price 列表\n",
    "            price.append(task_price)\n",
    "        \n",
    "        # 更新 '预测合理价格' 列\n",
    "        X_new['任务标价'] = price\n",
    "        \n",
    "        # 使用训练好的模型进行预测\n",
    "        y_new_pred = model.predict(X_new)\n",
    "        \n",
    "        # 将新的预测结果存储到 df_new_task 的 '预测任务执行情况' 列中\n",
    "        df_new_task['预测任务执行情况'] = y_new_pred\n",
    "        \n",
    "        # 计算当前配置下预测的任务执行情况的总和（比如任务完成的数量）\n",
    "        res = df_new_task['预测任务执行情况'].sum()\n",
    "        \n",
    "        # 更新最佳参数和结果\n",
    "        if res > best_res:\n",
    "            best_k = k\n",
    "            best_w = w\n",
    "            best_res = res\n",
    "\n",
    "print('最好的 k 为：', best_k, 'w 为：', best_w, 'res 为：', best_res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "5d057305-8108-482f-89d7-688ce08ff9e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6562874251497006\n"
     ]
    }
   ],
   "source": [
    "# 完成率\n",
    "print(548/835)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62cd9ef0-e832-4945-9b2e-2d1e27bd08f7",
   "metadata": {},
   "source": [
    "换模型，发现其实不咋样，还是随机森林好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "fc0d0068-b4ce-48b7-beb6-67dcae28ad8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "\n",
    "# 1. 准备数据\n",
    "# X 是特征集，选择 '任务标价'、'会员因子分数' 和 '竞争因子分数' 列\n",
    "X = df_new_task[['任务gps纬度', '任务gps经度', '任务标价', '会员因子分数', '竞争因子分数']]\n",
    "\n",
    "# y 是目标变量，选择 '任务执行情况' 列\n",
    "y = df_new_task['任务执行情况']\n",
    "\n",
    "# 2. 数据分割为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a7202bd8-2ac2-4f4d-ac50-7041a1786f20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>任务号码</th>\n",
       "      <th>任务gps纬度</th>\n",
       "      <th>任务gps经度</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>任务执行情况</th>\n",
       "      <th>任务标价</th>\n",
       "      <th>会员因子分数</th>\n",
       "      <th>竞争因子分数</th>\n",
       "      <th>预测任务执行情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0001</td>\n",
       "      <td>22.566142</td>\n",
       "      <td>113.980837</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0</td>\n",
       "      <td>66.199378</td>\n",
       "      <td>-0.175175</td>\n",
       "      <td>0.415050</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A0002</td>\n",
       "      <td>22.686205</td>\n",
       "      <td>113.940525</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>66.306500</td>\n",
       "      <td>0.023039</td>\n",
       "      <td>0.238261</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A0003</td>\n",
       "      <td>22.576512</td>\n",
       "      <td>113.957198</td>\n",
       "      <td>65.5</td>\n",
       "      <td>1</td>\n",
       "      <td>64.492029</td>\n",
       "      <td>-0.302747</td>\n",
       "      <td>0.201153</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A0004</td>\n",
       "      <td>22.564841</td>\n",
       "      <td>114.244571</td>\n",
       "      <td>75.0</td>\n",
       "      <td>0</td>\n",
       "      <td>65.570746</td>\n",
       "      <td>-0.001924</td>\n",
       "      <td>0.116073</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A0005</td>\n",
       "      <td>22.558888</td>\n",
       "      <td>113.950723</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>62.346242</td>\n",
       "      <td>-0.825690</td>\n",
       "      <td>0.294938</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>A0829</td>\n",
       "      <td>23.179030</td>\n",
       "      <td>112.876192</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>65.572724</td>\n",
       "      <td>-0.054424</td>\n",
       "      <td>0.168969</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>653</th>\n",
       "      <td>A0830</td>\n",
       "      <td>23.123411</td>\n",
       "      <td>113.151775</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>62.682655</td>\n",
       "      <td>-0.546518</td>\n",
       "      <td>0.083049</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>654</th>\n",
       "      <td>A0831</td>\n",
       "      <td>23.044062</td>\n",
       "      <td>113.125784</td>\n",
       "      <td>65.5</td>\n",
       "      <td>0</td>\n",
       "      <td>67.427490</td>\n",
       "      <td>-0.138593</td>\n",
       "      <td>0.624091</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>655</th>\n",
       "      <td>A0832</td>\n",
       "      <td>22.833262</td>\n",
       "      <td>113.280152</td>\n",
       "      <td>72.0</td>\n",
       "      <td>1</td>\n",
       "      <td>61.998482</td>\n",
       "      <td>-0.681070</td>\n",
       "      <td>0.080766</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>656</th>\n",
       "      <td>A0835</td>\n",
       "      <td>23.123294</td>\n",
       "      <td>113.110382</td>\n",
       "      <td>85.0</td>\n",
       "      <td>1</td>\n",
       "      <td>66.458909</td>\n",
       "      <td>0.028944</td>\n",
       "      <td>0.262838</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>657 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      任务号码    任务gps纬度     任务gps经度  任务标价  任务执行情况       任务标价    会员因子分数  \\\n",
       "0    A0001  22.566142  113.980837  66.0       0  66.199378 -0.175175   \n",
       "1    A0002  22.686205  113.940525  65.5       0  66.306500  0.023039   \n",
       "2    A0003  22.576512  113.957198  65.5       1  64.492029 -0.302747   \n",
       "3    A0004  22.564841  114.244571  75.0       0  65.570746 -0.001924   \n",
       "4    A0005  22.558888  113.950723  65.5       0  62.346242 -0.825690   \n",
       "..     ...        ...         ...   ...     ...        ...       ...   \n",
       "652  A0829  23.179030  112.876192  80.0       1  65.572724 -0.054424   \n",
       "653  A0830  23.123411  113.151775  85.0       1  62.682655 -0.546518   \n",
       "654  A0831  23.044062  113.125784  65.5       0  67.427490 -0.138593   \n",
       "655  A0832  22.833262  113.280152  72.0       1  61.998482 -0.681070   \n",
       "656  A0835  23.123294  113.110382  85.0       1  66.458909  0.028944   \n",
       "\n",
       "       竞争因子分数  预测任务执行情况  \n",
       "0    0.415050         0  \n",
       "1    0.238261         0  \n",
       "2    0.201153         0  \n",
       "3    0.116073         0  \n",
       "4    0.294938         0  \n",
       "..        ...       ...  \n",
       "652  0.168969         1  \n",
       "653  0.083049         1  \n",
       "654  0.624091         0  \n",
       "655  0.080766         1  \n",
       "656  0.262838         1  \n",
       "\n",
       "[657 rows x 9 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c9a2c4fc-b485-44cf-806b-fc2d622c5dcc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Logistic Regression...\n",
      "Accuracy for Logistic Regression: 0.6666666666666666\n",
      "Classification Report for Logistic Regression:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.70      0.59      0.64        66\n",
      "           1       0.64      0.74      0.69        66\n",
      "\n",
      "    accuracy                           0.67       132\n",
      "   macro avg       0.67      0.67      0.66       132\n",
      "weighted avg       0.67      0.67      0.66       132\n",
      "\n",
      "\n",
      "Training Support Vector Machine...\n",
      "Accuracy for Support Vector Machine: 0.5\n",
      "Classification Report for Support Vector Machine:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.00      0.00      0.00        66\n",
      "           1       0.50      1.00      0.67        66\n",
      "\n",
      "    accuracy                           0.50       132\n",
      "   macro avg       0.25      0.50      0.33       132\n",
      "weighted avg       0.25      0.50      0.33       132\n",
      "\n",
      "\n",
      "Training K-Nearest Neighbors...\n",
      "Accuracy for K-Nearest Neighbors: 0.7045454545454546\n",
      "Classification Report for K-Nearest Neighbors:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.75      0.62      0.68        66\n",
      "           1       0.68      0.79      0.73        66\n",
      "\n",
      "    accuracy                           0.70       132\n",
      "   macro avg       0.71      0.70      0.70       132\n",
      "weighted avg       0.71      0.70      0.70       132\n",
      "\n",
      "\n",
      "Training Gradient Boosting...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\develop\\miniconda3\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:469: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n",
      "C:\\develop\\miniconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1531: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n",
      "C:\\develop\\miniconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1531: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n",
      "C:\\develop\\miniconda3\\Lib\\site-packages\\sklearn\\metrics\\_classification.py:1531: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
      "  _warn_prf(average, modifier, f\"{metric.capitalize()} is\", len(result))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy for Gradient Boosting: 0.7272727272727273\n",
      "Classification Report for Gradient Boosting:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.73      0.71      0.72        66\n",
      "           1       0.72      0.74      0.73        66\n",
      "\n",
      "    accuracy                           0.73       132\n",
      "   macro avg       0.73      0.73      0.73       132\n",
      "weighted avg       0.73      0.73      0.73       132\n",
      "\n",
      "\n",
      "Training XGBoost...\n"
     ]
    },
    {
     "ename": "XGBoostError",
     "evalue": "[11:02:03] C:\\buildkite-agent\\builds\\buildkite-windows-cpu-autoscaling-group-i-0015a694724fa8361-1\\xgboost\\xgboost-ci-windows\\src\\data\\array_interface.h:218: Check failed: m == 1 || n == 1: ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mXGBoostError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[44], line 30\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, model \u001b[38;5;129;01min\u001b[39;00m models\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m     29\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTraining \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 30\u001b[0m     model\u001b[38;5;241m.\u001b[39mfit(X_train, y_train)\n\u001b[0;32m     31\u001b[0m     y_pred \u001b[38;5;241m=\u001b[39m model\u001b[38;5;241m.\u001b[39mpredict(X_test)\n\u001b[0;32m     33\u001b[0m     accuracy \u001b[38;5;241m=\u001b[39m accuracy_score(y_test, y_pred)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:726\u001b[0m, in \u001b[0;36mrequire_keyword_args.<locals>.throw_if.<locals>.inner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    724\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, arg \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(sig\u001b[38;5;241m.\u001b[39mparameters, args):\n\u001b[0;32m    725\u001b[0m     kwargs[k] \u001b[38;5;241m=\u001b[39m arg\n\u001b[1;32m--> 726\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\sklearn.py:1512\u001b[0m, in \u001b[0;36mXGBClassifier.fit\u001b[1;34m(self, X, y, sample_weight, base_margin, eval_set, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights)\u001b[0m\n\u001b[0;32m   1509\u001b[0m     params[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnum_class\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn_classes_\n\u001b[0;32m   1511\u001b[0m model, metric, params \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_configure_fit(xgb_model, params)\n\u001b[1;32m-> 1512\u001b[0m train_dmatrix, evals \u001b[38;5;241m=\u001b[39m _wrap_evaluation_matrices(\n\u001b[0;32m   1513\u001b[0m     missing\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmissing,\n\u001b[0;32m   1514\u001b[0m     X\u001b[38;5;241m=\u001b[39mX,\n\u001b[0;32m   1515\u001b[0m     y\u001b[38;5;241m=\u001b[39my,\n\u001b[0;32m   1516\u001b[0m     group\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1517\u001b[0m     qid\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1518\u001b[0m     sample_weight\u001b[38;5;241m=\u001b[39msample_weight,\n\u001b[0;32m   1519\u001b[0m     base_margin\u001b[38;5;241m=\u001b[39mbase_margin,\n\u001b[0;32m   1520\u001b[0m     feature_weights\u001b[38;5;241m=\u001b[39mfeature_weights,\n\u001b[0;32m   1521\u001b[0m     eval_set\u001b[38;5;241m=\u001b[39meval_set,\n\u001b[0;32m   1522\u001b[0m     sample_weight_eval_set\u001b[38;5;241m=\u001b[39msample_weight_eval_set,\n\u001b[0;32m   1523\u001b[0m     base_margin_eval_set\u001b[38;5;241m=\u001b[39mbase_margin_eval_set,\n\u001b[0;32m   1524\u001b[0m     eval_group\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1525\u001b[0m     eval_qid\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1526\u001b[0m     create_dmatrix\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_create_dmatrix,\n\u001b[0;32m   1527\u001b[0m     enable_categorical\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39menable_categorical,\n\u001b[0;32m   1528\u001b[0m     feature_types\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfeature_types,\n\u001b[0;32m   1529\u001b[0m )\n\u001b[0;32m   1531\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_Booster \u001b[38;5;241m=\u001b[39m train(\n\u001b[0;32m   1532\u001b[0m     params,\n\u001b[0;32m   1533\u001b[0m     train_dmatrix,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1542\u001b[0m     callbacks\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcallbacks,\n\u001b[0;32m   1543\u001b[0m )\n\u001b[0;32m   1545\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mcallable\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobjective):\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\sklearn.py:596\u001b[0m, in \u001b[0;36m_wrap_evaluation_matrices\u001b[1;34m(missing, X, y, group, qid, sample_weight, base_margin, feature_weights, eval_set, sample_weight_eval_set, base_margin_eval_set, eval_group, eval_qid, create_dmatrix, enable_categorical, feature_types)\u001b[0m\n\u001b[0;32m    576\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_wrap_evaluation_matrices\u001b[39m(\n\u001b[0;32m    577\u001b[0m     missing: \u001b[38;5;28mfloat\u001b[39m,\n\u001b[0;32m    578\u001b[0m     X: Any,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    592\u001b[0m     feature_types: Optional[FeatureTypes],\n\u001b[0;32m    593\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tuple[Any, List[Tuple[Any, \u001b[38;5;28mstr\u001b[39m]]]:\n\u001b[0;32m    594\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Convert array_like evaluation matrices into DMatrix.  Perform validation on the\u001b[39;00m\n\u001b[0;32m    595\u001b[0m \u001b[38;5;124;03m    way.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 596\u001b[0m     train_dmatrix \u001b[38;5;241m=\u001b[39m create_dmatrix(\n\u001b[0;32m    597\u001b[0m         data\u001b[38;5;241m=\u001b[39mX,\n\u001b[0;32m    598\u001b[0m         label\u001b[38;5;241m=\u001b[39my,\n\u001b[0;32m    599\u001b[0m         group\u001b[38;5;241m=\u001b[39mgroup,\n\u001b[0;32m    600\u001b[0m         qid\u001b[38;5;241m=\u001b[39mqid,\n\u001b[0;32m    601\u001b[0m         weight\u001b[38;5;241m=\u001b[39msample_weight,\n\u001b[0;32m    602\u001b[0m         base_margin\u001b[38;5;241m=\u001b[39mbase_margin,\n\u001b[0;32m    603\u001b[0m         feature_weights\u001b[38;5;241m=\u001b[39mfeature_weights,\n\u001b[0;32m    604\u001b[0m         missing\u001b[38;5;241m=\u001b[39mmissing,\n\u001b[0;32m    605\u001b[0m         enable_categorical\u001b[38;5;241m=\u001b[39menable_categorical,\n\u001b[0;32m    606\u001b[0m         feature_types\u001b[38;5;241m=\u001b[39mfeature_types,\n\u001b[0;32m    607\u001b[0m         ref\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m    608\u001b[0m     )\n\u001b[0;32m    610\u001b[0m     n_validation \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m eval_set \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(eval_set)\n\u001b[0;32m    612\u001b[0m     \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mvalidate_or_none\u001b[39m(meta: Optional[Sequence], name: \u001b[38;5;28mstr\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Sequence:\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\sklearn.py:1003\u001b[0m, in \u001b[0;36mXGBModel._create_dmatrix\u001b[1;34m(self, ref, **kwargs)\u001b[0m\n\u001b[0;32m   1001\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _can_use_qdm(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtree_method) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbooster \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgblinear\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m   1002\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1003\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m QuantileDMatrix(\n\u001b[0;32m   1004\u001b[0m             \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs, ref\u001b[38;5;241m=\u001b[39mref, nthread\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn_jobs, max_bin\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmax_bin\n\u001b[0;32m   1005\u001b[0m         )\n\u001b[0;32m   1006\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:  \u001b[38;5;66;03m# `QuantileDMatrix` supports lesser types than DMatrix\u001b[39;00m\n\u001b[0;32m   1007\u001b[0m         \u001b[38;5;28;01mpass\u001b[39;00m\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:726\u001b[0m, in \u001b[0;36mrequire_keyword_args.<locals>.throw_if.<locals>.inner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    724\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, arg \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(sig\u001b[38;5;241m.\u001b[39mparameters, args):\n\u001b[0;32m    725\u001b[0m     kwargs[k] \u001b[38;5;241m=\u001b[39m arg\n\u001b[1;32m--> 726\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:1573\u001b[0m, in \u001b[0;36mQuantileDMatrix.__init__\u001b[1;34m(self, data, label, weight, base_margin, missing, silent, feature_names, feature_types, nthread, max_bin, ref, group, qid, label_lower_bound, label_upper_bound, feature_weights, enable_categorical, data_split_mode)\u001b[0m\n\u001b[0;32m   1553\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28many\u001b[39m(\n\u001b[0;32m   1554\u001b[0m         info \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1555\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m info \u001b[38;5;129;01min\u001b[39;00m (\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1566\u001b[0m         )\n\u001b[0;32m   1567\u001b[0m     ):\n\u001b[0;32m   1568\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m   1569\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIf data iterator is used as input, data like label should be \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1570\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mspecified as batch argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1571\u001b[0m         )\n\u001b[1;32m-> 1573\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_init(\n\u001b[0;32m   1574\u001b[0m     data,\n\u001b[0;32m   1575\u001b[0m     ref\u001b[38;5;241m=\u001b[39mref,\n\u001b[0;32m   1576\u001b[0m     label\u001b[38;5;241m=\u001b[39mlabel,\n\u001b[0;32m   1577\u001b[0m     weight\u001b[38;5;241m=\u001b[39mweight,\n\u001b[0;32m   1578\u001b[0m     base_margin\u001b[38;5;241m=\u001b[39mbase_margin,\n\u001b[0;32m   1579\u001b[0m     group\u001b[38;5;241m=\u001b[39mgroup,\n\u001b[0;32m   1580\u001b[0m     qid\u001b[38;5;241m=\u001b[39mqid,\n\u001b[0;32m   1581\u001b[0m     label_lower_bound\u001b[38;5;241m=\u001b[39mlabel_lower_bound,\n\u001b[0;32m   1582\u001b[0m     label_upper_bound\u001b[38;5;241m=\u001b[39mlabel_upper_bound,\n\u001b[0;32m   1583\u001b[0m     feature_weights\u001b[38;5;241m=\u001b[39mfeature_weights,\n\u001b[0;32m   1584\u001b[0m     feature_names\u001b[38;5;241m=\u001b[39mfeature_names,\n\u001b[0;32m   1585\u001b[0m     feature_types\u001b[38;5;241m=\u001b[39mfeature_types,\n\u001b[0;32m   1586\u001b[0m     enable_categorical\u001b[38;5;241m=\u001b[39menable_categorical,\n\u001b[0;32m   1587\u001b[0m )\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:1632\u001b[0m, in \u001b[0;36mQuantileDMatrix._init\u001b[1;34m(self, data, ref, enable_categorical, **meta)\u001b[0m\n\u001b[0;32m   1620\u001b[0m config \u001b[38;5;241m=\u001b[39m make_jcargs(\n\u001b[0;32m   1621\u001b[0m     nthread\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnthread, missing\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmissing, max_bin\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmax_bin\n\u001b[0;32m   1622\u001b[0m )\n\u001b[0;32m   1623\u001b[0m ret \u001b[38;5;241m=\u001b[39m _LIB\u001b[38;5;241m.\u001b[39mXGQuantileDMatrixCreateFromCallback(\n\u001b[0;32m   1624\u001b[0m     \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   1625\u001b[0m     it\u001b[38;5;241m.\u001b[39mproxy\u001b[38;5;241m.\u001b[39mhandle,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1630\u001b[0m     ctypes\u001b[38;5;241m.\u001b[39mbyref(handle),\n\u001b[0;32m   1631\u001b[0m )\n\u001b[1;32m-> 1632\u001b[0m it\u001b[38;5;241m.\u001b[39mreraise()\n\u001b[0;32m   1633\u001b[0m \u001b[38;5;66;03m# delay check_call to throw intermediate exception first\u001b[39;00m\n\u001b[0;32m   1634\u001b[0m _check_call(ret)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:569\u001b[0m, in \u001b[0;36mDataIter.reraise\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    567\u001b[0m exc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_exception\n\u001b[0;32m    568\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_exception \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m--> 569\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:550\u001b[0m, in \u001b[0;36mDataIter._handle_exception\u001b[1;34m(self, fn, dft_ret)\u001b[0m\n\u001b[0;32m    547\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m dft_ret\n\u001b[0;32m    549\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 550\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m fn()\n\u001b[0;32m    551\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:  \u001b[38;5;66;03m# pylint: disable=broad-except\u001b[39;00m\n\u001b[0;32m    552\u001b[0m     \u001b[38;5;66;03m# Defer the exception in order to return 0 and stop the iteration.\u001b[39;00m\n\u001b[0;32m    553\u001b[0m     \u001b[38;5;66;03m# Exception inside a ctype callback function has no effect except\u001b[39;00m\n\u001b[0;32m    554\u001b[0m     \u001b[38;5;66;03m# for printing to stderr (doesn't stop the execution).\u001b[39;00m\n\u001b[0;32m    555\u001b[0m     tb \u001b[38;5;241m=\u001b[39m sys\u001b[38;5;241m.\u001b[39mexc_info()[\u001b[38;5;241m2\u001b[39m]\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:637\u001b[0m, in \u001b[0;36mDataIter._next_wrapper.<locals>.<lambda>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    635\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_temporary_data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m    636\u001b[0m \u001b[38;5;66;03m# pylint: disable=not-callable\u001b[39;00m\n\u001b[1;32m--> 637\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle_exception(\u001b[38;5;28;01mlambda\u001b[39;00m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnext(input_data), \u001b[38;5;241m0\u001b[39m)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\data.py:1416\u001b[0m, in \u001b[0;36mSingleBatchInternalIter.next\u001b[1;34m(self, input_data)\u001b[0m\n\u001b[0;32m   1414\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m   1415\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mit \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m-> 1416\u001b[0m input_data(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mkwargs)\n\u001b[0;32m   1417\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:726\u001b[0m, in \u001b[0;36mrequire_keyword_args.<locals>.throw_if.<locals>.inner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    724\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, arg \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(sig\u001b[38;5;241m.\u001b[39mparameters, args):\n\u001b[0;32m    725\u001b[0m     kwargs[k] \u001b[38;5;241m=\u001b[39m arg\n\u001b[1;32m--> 726\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:625\u001b[0m, in \u001b[0;36mDataIter._next_wrapper.<locals>.input_data\u001b[1;34m(data, feature_names, feature_types, **kwargs)\u001b[0m\n\u001b[0;32m    623\u001b[0m \u001b[38;5;66;03m# Stage the data, meta info are copied inside C++ MetaInfo.\u001b[39;00m\n\u001b[0;32m    624\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_temporary_data \u001b[38;5;241m=\u001b[39m (new, cat_codes, feature_names, feature_types)\n\u001b[1;32m--> 625\u001b[0m dispatch_proxy_set_data(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mproxy, new, cat_codes)\n\u001b[0;32m    626\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mproxy\u001b[38;5;241m.\u001b[39mset_info(\n\u001b[0;32m    627\u001b[0m     feature_names\u001b[38;5;241m=\u001b[39mfeature_names,\n\u001b[0;32m    628\u001b[0m     feature_types\u001b[38;5;241m=\u001b[39mfeature_types,\n\u001b[0;32m    629\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m    630\u001b[0m )\n\u001b[0;32m    631\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_data_ref \u001b[38;5;241m=\u001b[39m ref\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\data.py:1492\u001b[0m, in \u001b[0;36mdispatch_proxy_set_data\u001b[1;34m(proxy, data, cat_codes)\u001b[0m\n\u001b[0;32m   1490\u001b[0m \u001b[38;5;66;03m# Host\u001b[39;00m\n\u001b[0;32m   1491\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, PandasTransformed):\n\u001b[1;32m-> 1492\u001b[0m     proxy\u001b[38;5;241m.\u001b[39m_ref_data_from_pandas(data)  \u001b[38;5;66;03m# pylint: disable=W0212\u001b[39;00m\n\u001b[0;32m   1493\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m   1494\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _is_np_array_like(data):\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:1466\u001b[0m, in \u001b[0;36m_ProxyDMatrix._ref_data_from_pandas\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m   1464\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_ref_data_from_pandas\u001b[39m(\u001b[38;5;28mself\u001b[39m, data: DataType) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1465\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Reference data from a pandas DataFrame. The input is a PandasTransformed instance.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 1466\u001b[0m     _check_call(\n\u001b[0;32m   1467\u001b[0m         _LIB\u001b[38;5;241m.\u001b[39mXGProxyDMatrixSetDataColumnar(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandle, data\u001b[38;5;241m.\u001b[39marray_interface())\n\u001b[0;32m   1468\u001b[0m     )\n",
      "File \u001b[1;32mC:\\develop\\miniconda3\\Lib\\site-packages\\xgboost\\core.py:284\u001b[0m, in \u001b[0;36m_check_call\u001b[1;34m(ret)\u001b[0m\n\u001b[0;32m    273\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Check the return value of C API call\u001b[39;00m\n\u001b[0;32m    274\u001b[0m \n\u001b[0;32m    275\u001b[0m \u001b[38;5;124;03mThis function will raise exception when error occurs.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    281\u001b[0m \u001b[38;5;124;03m    return value from API calls\u001b[39;00m\n\u001b[0;32m    282\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    283\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m ret \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m--> 284\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m XGBoostError(py_str(_LIB\u001b[38;5;241m.\u001b[39mXGBGetLastError()))\n",
      "\u001b[1;31mXGBoostError\u001b[0m: [11:02:03] C:\\buildkite-agent\\builds\\buildkite-windows-cpu-autoscaling-group-i-0015a694724fa8361-1\\xgboost\\xgboost-ci-windows\\src\\data\\array_interface.h:218: Check failed: m == 1 || n == 1: "
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score, classification_report\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "import xgboost as xgb\n",
    "import lightgbm as lgb\n",
    "\n",
    "# 1. 准备数据\n",
    "X = df_new_task[['任务gps纬度', '任务gps经度', '任务标价', '会员因子分数', '竞争因子分数']]\n",
    "y = df_new_task['任务执行情况']\n",
    "\n",
    "# 2. 数据分割为训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 3. 模型列表\n",
    "models = {\n",
    "    \"Logistic Regression\": LogisticRegression(random_state=42),\n",
    "    \"Support Vector Machine\": SVC(random_state=42),\n",
    "    \"K-Nearest Neighbors\": KNeighborsClassifier(),\n",
    "    \"Gradient Boosting\": GradientBoostingClassifier(random_state=42),\n",
    "    \"XGBoost\": xgb.XGBClassifier(random_state=42),\n",
    "    \"LightGBM\": lgb.LGBMClassifier(random_state=42)\n",
    "}\n",
    "\n",
    "# 4. 训练和评估每个模型\n",
    "for name, model in models.items():\n",
    "    print(f\"Training {name}...\")\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict(X_test)\n",
    "    \n",
    "    accuracy = accuracy_score(y_test, y_pred)\n",
    "    report = classification_report(y_test, y_pred)\n",
    "    \n",
    "    print(f\"Accuracy for {name}: {accuracy}\")\n",
    "    print(f\"Classification Report for {name}:\\n{report}\\n\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
